package utils;

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

public class CopyDocx {
    public static void main(String[] args) {
        String source_docx="/Users/c7/Desktop/企业诊断/对标企业/典型案例-电子产品类-霍尼韦尔公司案例20230429.docx";
        String target_docx="/Users/c7/Desktop/企业诊断/3.诊断报告-数字化应用.docx";
        copyDocx(source_docx,target_docx);
    }
    public  static void  copyDocx(String input,String target){
        //加载文档1
        Document doc1 = new Document();
        doc1.loadFromFile(input);


        //加载文档2
        Document doc2 = new Document();
        doc2.loadFromFile(target);
        System.out.println(doc2.getSections().getCount());
        System.out.println(doc2.getSections().get(0).getParagraphs().getCount());
        System.out.println(doc1.getSections().get(0).getParagraphs().getCount());
        //遍历文档1中的所有子对象
        for (int i = 0; i < doc1.getSections().getCount(); i++) {
            Section section = doc1.getSections().get(i);

            for( int j = section.getParagraphs().getCount()-1;j> 0;j--)
            {
                Object object = section.getBody().getChildObjects().get(j);

                System.out.println(j);
                Paragraph paragraph=section.getParagraphs().get(j);
                Paragraph newparagraph = (Paragraph) paragraph.deepClone();
                //复制文档1中的正文内容添加到文档2
                doc2.getSections().get(0).getParagraphs().insert(205,newparagraph);
              //  doc2.getSections().get(0).getBody().getChildObjects().add(((DocumentObject) object).deepClone());
            }
        }

        //保存文档2
        doc2.saveToFile("/Users/c7/Desktop/CopyDoc.docx");
        doc2.dispose();
        restWord("/Users/c7/Desktop/CopyDoc.docx");
    }

    private static void restWord(String docFilePath) {
        try (FileInputStream in = new FileInputStream(docFilePath)) {
            XWPFDocument doc = new XWPFDocument(OPCPackage.open(in));
            List<XWPFParagraph> paragraphs = doc.getParagraphs();
            if (paragraphs.size() < 1) return;
            XWPFParagraph firstParagraph = paragraphs.get(0);
            if (firstParagraph.getText().contains("Spire.Doc")) {
                doc.removeBodyElement(doc.getPosOfParagraph(firstParagraph));
            }
            OutputStream out = new FileOutputStream(docFilePath);
            doc.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyDocx2(String input,String target){
        //创建两个 Document 类的对象顶分别载入 Word 文档
        Document document2 = new Document(input);
        Document document1 = new Document(target);

        //在第二个文档中循环获取所有节
        for (Object sectionObj : (Iterable) document2.getSections()) {
            Section sec=(Section)sectionObj;
            //在所有节中循环获取所有子对象
            for (Object docObj :(Iterable ) sec.getBody().getChildObjects()) {
                DocumentObject obj=(DocumentObject)docObj;

                //获取第一个文档的最后一节
                Section lastSection = document1.getLastSection();
                Body body = lastSection.getBody();
                body.getChildObjects().add(obj.deepClone());
//                TextSelection lastSection = document1.findString("<%target%>",false,true);
//                TextRange range = lastSection.getAsOneRange(true);
//                range.getOwner().getChildObjects().add(obj.deepClone());
//                //将所有子对象添加到第一个文档的最后一节中
//                Body body = lastSection.getBody();
//                body.getChildObjects().add(obj.deepClone());
            }
        }

        //保存结果文档
        document1.saveToFile("D:\\MergingResult2.docx", FileFormat.Docx_2013);

    }
}



